﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.Pptx;
using System.IO;
using MediaMonitor.Common;

namespace PPTUtilities
{
    class EmbedMedia
    {
        public static void AddVideo(PresentationEx pres, int slideposition, int Xposition, int Yposition, int width, int height, string VideoFileName, string ImagePath, bool IsEmbedded)
        {
            SlideEx sld = pres.Slides[slideposition];

            FileStream fstr = null;
            if (!string.IsNullOrEmpty(VideoFileName) && VideoFileName != HttpContext.Current.Server.MapPath("../Clip/") && File.Exists(VideoFileName) && Path.GetExtension(VideoFileName).ToLower() == ".wmv")
            {
                VideoEx vid = null;
                if (IsEmbedded)
                {
                    fstr = new FileStream(VideoFileName, FileMode.Open, FileAccess.Read);
                    vid = pres.Videos.AddVideo(fstr);
                }
                sld.Shapes.AddVideoFrame(Xposition, Yposition, width, height, VideoFileName);
                VideoFrameEx vf = sld.Shapes.OfType<VideoFrameEx>().First();
                if (!string.IsNullOrEmpty(ImagePath) && ImagePath != HttpContext.Current.Server.MapPath("../Image/") && File.Exists(ImagePath))
                {
                    fstr = new FileStream(ImagePath, FileMode.Open, FileAccess.Read);
                    ImageEx imgx = pres.Images.AddImage(fstr);
                    vf.PictureFormat.Picture.Image = imgx;
                }
                vf.PlayMode = VideoPlayModePresetEx.OnClick;
                vf.Volume = AudioVolumeModeEx.Loud;
                if (IsEmbedded)
                    vf.EmbeddedVideo = vid;
            }
            else if (!string.IsNullOrEmpty(VideoFileName) && VideoFileName != HttpContext.Current.Server.MapPath("../Clip/") && File.Exists(VideoFileName))
            {
                AudioFrameEx af = null;
                if (IsEmbedded)
                {
                    fstr = new FileStream(VideoFileName, FileMode.Open, FileAccess.Read);
                    af = sld.Shapes.AddAudioFrameEmbedded(Xposition, Yposition, width, height, fstr);
                }
                else
                    af = sld.Shapes.AddAudioFrameLinked(Xposition, Yposition, width, height, VideoFileName);
                if (!string.IsNullOrEmpty(ImagePath) && ImagePath !=HttpContext.Current.Server.MapPath("../Image/") && File.Exists(ImagePath))
                {
                    fstr = new FileStream(ImagePath, FileMode.Open, FileAccess.Read);
                    ImageEx imgx = pres.Images.AddImage(fstr);
                    af.PictureFormat.Picture.Image = imgx;
                }
                //Set Play Mode and Volume of the Audio
                af.PlayMode = AudioPlayModePresetEx.OnClick;
                af.Volume = AudioVolumeModeEx.Loud;
            }
            else if (!string.IsNullOrEmpty(ImagePath) && ImagePath != HttpContext.Current.Server.MapPath("../Image/") && File.Exists(ImagePath))
            {
                fstr = new FileStream(ImagePath, FileMode.Open, FileAccess.Read);
                ImageEx imgx = pres.Images.AddImage(fstr);
                //Add Picture Frame with height and width equivalent of Picture
                sld.Shapes.AddPictureFrame(ShapeTypeEx.Rectangle, Xposition, Yposition, width, height, imgx);
                ShapeEx shp = sld.Shapes.OfType<ShapeEx>().Last();
                shp.LineFormat.FillFormat.FillType = FillTypeEx.NoFill;
            }
        }
    }
}
